package E10.util;

import E10.UploadFile;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 史祥连
 * @Date 2024/1/29 9:27
 * @Version 1.0
 */
public class TokenMaker {
    private static String type ="release";

    private static String E10_API_URL_BASE_test = "https://api.yunteams.cn";
    private static String E10_API_URL_BASE_release = "https://api.mulinquan.cn";
    private static String E10_API_userid_test = "5116064451991577795";
    private static String E10_API_userid_release = "3086018350865928585";

    private static String app_key_release = "25fd00a1d41fe08be87a6dc9d7f28dc9";
    private static String app_secret_release = "bc21264099eac342e1f3b231877b969b";
    private static String Corpid_release = "b1bf4c491e3d2096ca0c657083839836";

    private static String app_key_test = "1a2f20f0773a839f43926227ad362dad";
    private static String Corpid_test = "6caeb52680ecddf055e83508c2f84371";
    private static String app_secret_test = "7e09d847c36d00bd82b9646e497c18ca";
    public static String loadAccessToken(){
        //https://weapp.e-cology.com.cn/openserver/oauth2/authorize?corpid=f65afca70cdab51533e1df83513b01ac&response_type=code&state=xifei

        String url = E10_API_URL_BASE_test+"/oauth2/authorize";
        // String url = "https://api.eteams.cn/oauth2/authorize";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("corpid", Corpid_test);
        paramMap.put("response_type", "code");
        paramMap.put("state", "123123");
        String result = HttpUtil.get(url, paramMap);
        System.out.println("1.1loadAccessToken-code-url::" + url);
        System.out.println("1.2loadAccessToken-code-RESP::" + result);
        UploadFile.ApiAccessTokenDTO dto = JsonHelper.parseJsonStringToObject(result, UploadFile.ApiAccessTokenDTO.class);
        String code = dto != null && dto.readIsSuccess()? dto.getCode() : "";
        System.out.println("1.3loadAccessToken-code-VALUE::" + code);
        String accessToken = null;
        if(!JsonHelper.isNullOrEmpty(code)){
//            TODO:正式
            url = E10_API_URL_BASE_test + "/oauth2/access_token";
//            TODO:测试
            // url = "https://api.eteams.cn/oauth2/access_token";
            paramMap = new HashMap<>();
            paramMap.put("app_key", app_key_test);
            paramMap.put("app_secret", app_secret_test);
            paramMap.put("grant_type", "authorization_code");
            paramMap.put("code", code);
            result = HttpUtil.post(url, paramMap);
            System.out.println("2.1loadAccessToken-token-url::" + url);
            System.out.println("2.2loadAccessToken-token-RESP::" + result);
            UploadFile.ApiAccessTokenDTO dto2 = JsonHelper.parseJsonStringToObject(result, UploadFile.ApiAccessTokenDTO.class);
            accessToken = dto2 != null && dto2.readIsSuccess()? dto2.getAccessToken() : "";
            System.out.println("2.3loadAccessToken-token-VALUE::" + accessToken);
        }
        return accessToken;
    }
    static class JsonHelper {
        public static boolean isNullOrEmpty(Object s){
            return s == null || "".equals(s);
        }
        /**
         * java 对象转Json字符串
         *
         * @param javaObject
         * @return
         */
        public static String parseObjectToJsonString(Object javaObject) {
            if (javaObject == null) {
                return null;
            }
            try {
//			byte[] jsonBytes = JSONObject.toJSONBytes(javaObject);
//			String s = new String(jsonBytes);
                String s = ((JSONObject) JSONObject.toJSON(javaObject)).toJSONString();
                return s;
            } catch (Exception e) {
                e.printStackTrace();
                //GolomeeLogger.exception(e.getStackTrace(), e.getMessage());
                return null;
            }
        }

        public static <T> T parseJsonStringToObject(String key, String jsonStr, Class<T> clazz) {
            if (jsonStr == null) {
                return null;
            }
            try {
                return JSON.parseObject(jsonStr, clazz);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("parseJsonStringToObject::" + key + "::" + jsonStr);
            }
            return null;
        }

        public static <T> T parseJsonStringToObject(String jsonStr, Class<T> clazz) {
            if (isNullOrEmpty(jsonStr)) {
                return null;
            }
            try {
                return JSON.parseObject(jsonStr, clazz);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("parseJsonStringToObject::" + jsonStr);
            }
            return null;
        }

        public static <T> List<T> parseJsonStringToListObject(String jsonStr, Class<T> clazz) {
            if (isNullOrEmpty(jsonStr)) {
                return null;
            }
            try {
                return JSONArray.parseArray(jsonStr, clazz);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("parseJsonStringToListObject::" + jsonStr);
            }
            return null;
        }

        /**
         * java 数组或列表转Json字符串
         *
         * @param javaObject
         * @return
         */
        public static String parseListToJsonString(Object javaObject) {
            if (javaObject == null) {
                return null;
            }
            try {
                String s = ((JSONArray) JSONArray.toJSON(javaObject)).toJSONString();
                return s;
            } catch (Exception e) {
                e.toString();
                return null;
            }
        }
    }
}
